<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *转让
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'转让列表'),
	array('op'=>'edit','title'=>'添加转让'),	
);
$return['status']=$this->goods_status;
$return['credit_types']=$this->credit_types;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']['type']=intval($_RQ['type']);
	$this->session('type',intval($_RQ['type']));
	if(!empty($_RQ['nickname'])){
		$data['uniacid']=$_SESSION['uniacid'];
		$data['nickname like']='%'.trim($_RQ['nickname']).'%';
		$mid=pdo_get('core_members',$data);
		$params['mid']=$mid['id'];
		$return['params']['mid']=$mid['id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['type'])){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_creditchange_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','mobile','avatar','nickname','username','sex'));
			$item['member']=$member;
			if($this->session('type')==1){
				$item['url']=custom_url('jy_creditchange/gochat//id='.$item['id'],true);
			}else{
				$item['url']=custom_url('jy_creditchange/gochat/weixin/id='.$item['id'],true);
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'转让ID','type'=>'string'),
			array('name'=>'title','title'=>'转让名称','type'=>'string'),
			array('type'=>'member','title'=>'发布人','name'=>'member'),
			array('name'=>'price','title'=>'转让价格','type'=>'string'),
			array('name'=>'type','title'=>'转让虚拟币类型','type'=>'radio','options'=>'credit_types'),
			array('name'=>'creditnum','title'=>'转让数量','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'发布时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'type','width'=>15,'title'=>'拟币类型','type'=>'radio','options'=>'credit_types'),
			array('name'=>'nickname','width'=>15,'title'=>'发布人昵称','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'转让标题','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_creditchange_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'));
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回转让列表'),
		),
		'edit'=>array(
					array('name'=>'title','title'=>'转让名称','type'=>'string'),
					array('name'=>'image','title'=>'转让封面','type'=>'image'),
					array('type'=>'choose','title'=>'选择发布人','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
					array('name'=>'desc','title'=>'转让介绍','type'=>'text'),
					array('name'=>'type','title'=>'转让虚拟币类型','type'=>'radio','options'=>'credit_types'),
					array('name'=>'creditnum','title'=>'转让数量','type'=>'string'),
					array('name'=>'price','title'=>'转让价格','type'=>'string'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['mid']){
		exi('请选择发布人！','error');
	}
	if(!$postdata['title']){
		exi('请填写转让名称！','error');
	}
	if(!$postdata['creditnum']){
		exi('请上传转让数量！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'type'=>intval($postdata['type']),
		'mid'=>intval($postdata['mid']),
		'desc'=>trim($postdata['desc']),
		'price'=>trim($postdata['price']),
		'status'=>intval($postdata['status']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_creditchange_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$data['creditnum']=intval($postdata['creditnum']);
		$this->model('member')->recharge($data['mid'],$data['creditnum'],$data['type']);
		pdo_insert('jy_creditchange_goods',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择转让！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_creditchange_goods',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_creditchange_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}